var list = [];
var height;
var app = getApp();

Page({
  data: {
    msg: '',
    lists: [],
    index: "",
    height: "400rpx",
    timer: {},
    globalData: "",
    scrollTop: 100,
    scrollHeight: ""
  },
  change(e) {
    //console.log(e,77);
    if (e) {
      this.setData({
        msg: e.detail.value
      })

    } else {
      //console.log(222);

      this.setData({
        msg: ""
      })
      return;
    }
  },
  send() {
    var This = this;

    if (!this.data.msg) {
      return;
    }

    /* 获取当前时间 */
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1; //真实的月份需要再加上1
    var day = now.getDate();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();


    var time = {};
    // time.year=year;
    // time.month=month>=10?month:"0"+month;
    // time.day=day>=10?day:"0"+day;
    time.hours = hours >= 10 ? hours : "0" + hours;
    time.minutes = minutes >= 10 ? minutes : "0" + minutes;
    // time.seconds=seconds>=10?seconds:"0"+seconds;



    /* websocket需要发送的数据 */

    wx.getStorage({
      key: 'user',
      success(res) {
        console.log(res, 677);
        if (res.errMsg == "getStorage:ok") {
          var message = res.data;
          time.avatarUrl = message.avatarUrl;
          time.msg = This.data.msg;

          list = This.data.lists;

          list.push(time);
          console.log(list, 8899);
          /* 将表单以及用户信息存入本地缓存 */
          wx.setStorage({
            key: 'key',
            data: JSON.stringify(list)
          })


          var obj = {
            nickName: message.nickName,
            gender: message.gender,
            avatarUrl: message.avatarUrl,
            uid: message.uid,
            msg: This.data.msg,
            flag: 2 //我是发弹幕进入的
          }
          console.log(obj, 666);
          /* 更改本地数据  用以显示 */
          This.setData({
            lists: list,
            height: height - 100,
            index: "",
            msg: "",
            scrollTop: This.data.scrollTop - height + 10
          });

          /* 如果是吓跑程序发送弹幕进入的话就websoket */
          if (obj.flag == 2) {
            wx.sendSocketMessage({
              data: JSON.stringify(obj),
            })
          }
        }
      }
    })

  },
  onShow(options) {
    this.setData({
      scrollTop:500
    })
  },
  onLoad: function (options) {

    /* 获取本地缓存显示数据 */
    var This = this;
    wx.getStorage({
      key: 'key',
      success(res) {
        console.log(res, 9999);
        This.setData({
          lists: JSON.parse(res.data) || [],
          height: height - 100,
          index: ""
        });

      }
    })
    // 获取用户信息
    // console.log(app.globalData.userInfo);
    // var  userInfo=app.globalData.userInfo;

    wx.onSocketOpen(res => {
      console.log('WebSocket连接已打开！');
      // 'Hello,World:' + Math.round(Math.random() * 0xFFFFFF).toString(),
      // wx.sendSocketMessage({
      //   data: this.data.msg
      // })
    })

    wx.onSocketMessage(function (res) {
      // console.log(res)
    })

    wx.onSocketClose(function (res) {
      //console.log('WebSocket连接已关闭！')
    })
  },


  /* 键盘弹起解决方案 */
  foucus: function (e) {
    var that = this;
    that.setData({
      bottom: e.detail.height
    })
  },
  //失去聚焦
  blur: function (e) {
    var that = this;

    that.setData({
      bottom: 0,
      scrollTop: that.data.scrollTop - height + 100
    })


  }
})